package com.lib.model.dao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;

import org.springframework.orm.ibatis.support.SqlMapClientDaoSupport;

import com.lib.model.dto.MyLendingDto;
import com.lib.model.dto.MyLendingListDto;
import com.lib.model.dto.MyReservationDto;

public class MyBookManageDao extends SqlMapClientDaoSupport implements IMyBookManageDao {

	@Override
	public ArrayList<MyLendingListDto> getAllMyLending(String id, int startPage,int startBlock,String searchType, String searchValue) {
		
		ArrayList<MyLendingListDto> list = null;
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("id", id);
		map.put("startpage", startPage);
		map.put("endpage", startBlock);
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);
		
		

			list = (ArrayList<MyLendingListDto>)getSqlMapClientTemplate().queryForList("MyBookManage.getAllMyLendingList",map);


		return list;
	}

	@Override
	public ArrayList<MyReservationDto> getMyReservation(String id) {
		
		ArrayList<MyReservationDto> list = null;

			list = (ArrayList<MyReservationDto>)getSqlMapClientTemplate().queryForList("MyBookManage.getMyReservation",id);

		return list;
	}

	@Override
	public ArrayList<MyLendingDto> getMyLending(String id) {
		ArrayList<MyLendingDto> list = null;
		

			list = (ArrayList<MyLendingDto>)getSqlMapClientTemplate().queryForList("MyBookManage.getMyLending",id);

		return list;
	}

	@Override
	public boolean myReservationCancel(String book_num,String startdate) {
		int count = 0;
		
		Map<String, String> map = new HashMap<String, String>();
		map.put("book_num", book_num);
		map.put("startdate", startdate);

		try {
			getSqlMapClientTemplate().delete("MyBookManage.myReservationCancel",map);

			count = 1;
		} catch (Exception e) {
			count = -1;

		}
		
		return count>0 ? true:false;
	}

	@Override
	public boolean updateCode(String book_num) {
		int count = 0;
		try {
			getSqlMapClientTemplate().update("MyBookManage.updateCode",book_num);

			count = 1;
		} catch (Exception e) {
			count = -1;

		}
		return count>0 ? true:false;
	}

	@Override
	public boolean updatePrearrdate(String book_num) {
		int count = 1;
		System.out.println(book_num);
		try {
				getSqlMapClientTemplate().update("MyBookManage.updatePrearrdate",book_num);
				count = 1;
		} catch (Exception e) {
			count =-1;

			
		}
		return count >0 ?true:false;
	}

	@Override
	public String checkReservationBook(String book_num) {
		String booknum = null;

			booknum = (String)getSqlMapClientTemplate().queryForObject("MyBookManage.checkReservationBook",book_num);

		return booknum;
	}

	@Override
	public int getAllLendingCount(String id,String searchType, String searchValue) {
		int count = 0;
		
		Map<String,Object> map = new HashMap<String, Object>();
		
		map.put("id", id);
		map.put("searchType", searchType);
		map.put("searchValue", searchValue);
		

			count = (Integer) getSqlMapClientTemplate().queryForObject("MyBookManage.getAllLendingCount",map);

		return count;
	}

	@Override
	public int chenkLending(String id) {
		return (Integer)getSqlMapClientTemplate().queryForObject("MyBookManage.chenkLending",id);
	}
	
	

}
